// new version of the window manager...can't make it worse. AMIRITE?

// Overide Windows minimize to move window inside dock  
Object.extend(Windows, {
  // Overide minimize function
  minimize: function(id, event) {
    var win = this.getWindow(id)
    if (win && win.visible) {
      // Hide current window
     var oldHideEffect = win.options.hideEffect;
     win.options.hideEffect = Element.hide; 
     win.hide();            
     win.options.hideEffect = oldHideEffect;
	 win.mimized = true;
     Windows.blur(win.getId());

    }
   // Event.stop(event);
  },                 

  // Restore function
  restore: function(event) { 
		var element = Event.element(event);
		// Show window
		element.win.show();                   
		element.win.toFront();   
  }


	
});

//var windowManager = {
// i probably can actually make it worse...something to think about for the future, surely.

function onTaskbarClick(event){
	var dockItem = Event.element(event);
	//alert(dockItem.win.getId());
	var window = Windows.getWindow(dockItem.win.getId());
	if(!window.visible){
	window.show();
	window.toFront();
	} else {
	Windows.minimize(dockItem.win.getId(),event);
	}
}

function menuClick(event){
var win = new Window({className: "mac_os_x", blurClassName: "blur_os_x", title: "Applications", width:250, height:150}); 
win.getContent().update('<h3><div id="applications">ApplicationsMenuHere</div></h3>');
win.setConstraint(true, {left:0, right:0, top: 0, bottom:0})
win.setDestroyOnClose();
win.show();
}

function initWM(){
var menuButton = $('appMenu');
menuButton = document.createElement("span");
menuButton.className = "appMenu";
menuButton.update("Apps");
$('dock').appendChild(menuButton);
Event.observe(menuButton, "mouseup", menuClick);
}

var taskbar = {

onShow: function(eventName, win) {
//alert(win.getId());
var dockItem = $("dockItem_" + win.getId());
if(win.dockItemLoaded != 1){
if(!dockItem){
      dockItem = document.createElement("span");
      dockItem.className = "dock_item";
	dockItem.id = "dock_item"; 
      dockItem.style.display = "none";
      dockItem.win = win;
      $('dock').appendChild(dockItem);
      Event.observe(dockItem, "mouseup", onTaskbarClick);
      $(dockItem).update(win.getTitle());
	  win.dockItem = dockItem;
      //new Effect.Appear(dockItem,{duration:0.1});
	dockItem.style.display = "";
	win.dockItemLoaded = 1;
}
} else {
//alert("YAY");
}
},

onDestroy: function(eventName, win){
	var dockItem = win.dockItem;
	if(dockItem){
		new Effect.Fade(dockItem, {duration:0.1,afterFinish: function() {dockItem.remove()}});
	}
},
}

Windows.addObserver(taskbar);
